<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
if($_RQ['op']=='main'){
	$return['setting']=$this->plugin_setting('page_setting');
	$params=array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	$return['commission_money']=$this->sum('order',$params,'price');
	if(!$return['commission_money']){
		$return['commission_money']=0;
	}
	$return['commission_total']=$this->count('order',$params);
	$return['list']=array();
	$return['params']=array(
		'status'=>intval($_RQ['status']),
		'ispay'=>intval($_RQ['ispay']),
	);
	$return['psize']=20;
	set_html('title','我的卖车');
	exi($return);
}
if($_RQ['op']=='list'){
	$params=array('agent_id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	if(intval($_RQ['status'])){
		if($_RQ['status']!=100){
			$params['status']=intval($_RQ['status']);	
		}
		if($_RQ['status']==1){
			$params['ispay']=1;	
		}
	}elseif(intval($_RQ['ispay'])){
		$params['ispay']=intval($_RQ['ispay']);
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('order',$parameter);
	if(!$list){
		exi('none');
	}
	foreach($list as &$item){
		$item['member']=model('core/member')->basic($item['mid']);
		if($item['ispay']==1 && !$item['status']){
			$item['status']=1;
			pdo_update('jy_newcar_order',array('status'=>1),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
		}
		$item['statustitle']=$this->order_status[$item['status']];	
		$item['createtime']=date('Y-m-d H:i',$item['createtime']);
		$orderid=pdo_getcolumn('jy_weishop_order',array('ordersn'=>$item['ordersn'],'uniacid'=>$_SESSION['uniacid']),'id');
		$ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$orderid,'uniacid'=>$_SESSION['uniacid']),array('goodsid','optionid','price','total','goodssn','productsn'));
		$merchs =array();
		$item['feemoney']=0;
		foreach($ordergoods as &$gv){
			$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','marketprice','unit','image','title','merchid','weight'));
			$goodsinfo['total']=$gv['total'];
			$goodsinfo['price']=$gv['price'];
			$goodsinfo['image']=$goodsinfo['image']?tomedia($goodsinfo['image']):'';
			$goodsinfo['weight']=$goodsinfo['weight']>0?$goodsinfo['weight']:pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$gv['goodsid']),'weight');
			//获取门店
			$stores = array();
			$goodsinfo['storeids'] = json_decode($goodsinfo['storeids'],true);
			if ($goodsinfo['isverify'] == 1) {
				if (empty($goodsinfo['storeids'])) {
					$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3)));
				}else {
					foreach($goodsinfo['storeids'] as $sv){
						$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3),'id'=>$sv['id']));
					}
				}
			}elseif($shopset['openstore'] ==1){
				if (empty($goodsinfo['storeids'])) {
					$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
				}else {
					foreach($goodsinfo['storeids'] as $sv){
						$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id'=>$sv['id']));
					}
				}
			}
			$goodsinfo['store']=$stores;
			//商品规格
			if($gv['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$gv['optionid']),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$goodsinfo['selespename']=implode("，",$specs);
			}
			//获取商户
			if($goodsinfo['merchid']){
				$merchinfo = pdo_get('jy_merch_user',array('id'=>$goodsinfo['merchid'],'uniacid'=>$_SESSION['uniacid']));
				$merchs[$goodsinfo['merchid']] = $merchinfo;
				$merchs[$goodsinfo['merchid']]['goods'][] = $goodsinfo;
			}else{
				$merchs[0]['goods'][] = $goodsinfo;
			}
		}
		$item['merchs']=$merchs;
	}
	exi($list);
}
if($_RQ['op']=='upprice'){
	$id=intval($_RQ['id']);
	$post=get_postdata();	
	if($post['price']){
		$post['orderid']=pdo_getcolumn('jy_weishop_order',array('ordersn'=>$post['ordersn'],'uniacid'=>$_SESSION['uniacid']),'id');
		if(!is_numeric($post['price'])){
			exi('价格必须是数字！','error');
		}
		if(!method_exists(cfc('pay'),'change_pay_fee')){
			exi('修改价格失败！请联系管理员，更新框架到最新','error');
		}
		$price=floatval($post['price']);
		if($price < 0){
			$price = 0;
		}
		model('jy_weishop/order')->upprice($post['orderid'],$price);
		$this->update('order',array('price'=>$price),array('id'=>$post['id'],'uniacid'=>$_SESSION['uniacid']));
		exi('改价成功！','','/fenxiao.order/');	
	}
	$item=$this->get('order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$htmls=array(
		'type'=>'edit',
		'desc'=>'修改价格',
	);
	$htmls['edit']=array(
		array('type'=>'string','title'=>'价格','name'=>'price','help'=>'修改价格！'),	
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='xunibalance'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1,'isxuni'=>1);
	$money=pdo_getsum('jy_newcar_commission',$params,'money');
	cfc('core')->member_credit($_SESSION['uid'],'credit'.$this->setting['credit_type'],$money,true,'jy_newcar','分佣奖励结算');
	$update=array(
		'status'=>2,
	);
	$this->update('commission',$update,$params);
	exi('结算成功！','','/fenxiao.index/');
}
?>